<template>
  <el-table
    style="width: 100%; margin-bottom: 20px"
    row-key="id"
    border
    :data="MenuList"
  >
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作" align="center">
      <template #="{ row }">
        <el-button
          type="primary"
          icon="Plus"
          @click="AddPermisstion(row)"
          size="small"
          :disabled="row.level == 4 ? true : false"
          v-has="`btn.Permission.add`"
          >{{
            row.level == 1 || row.level == 2 ? "添加菜单" : "添加功能"
          }}</el-button
        >
        <el-button
          type="info"
          icon="Edit"
          size="small"
          :disabled="row.level == 1 ? true : false"
          @click="updatePermisstion(row)"
          v-has="`btn.Permission.update`"
          >编辑</el-button
        >

        <el-popconfirm
          :title="`你确定要删除${row.name}吗?`"
          @confirm="DeleteMenu(row.id)"
          width="260px"
        >
          <template #reference>
            <el-button
              type="danger"
              icon="Delete"
              size="small"
              :disabled="row.level == 1 ? true : false"
              v-has="`btn.Permission.remove`"
              >删除</el-button
            >
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 对话框 -->
  <el-dialog
    v-model="dialogVisible"
    :title="MenuData.id ? '修改菜单' : '添加菜单'"
  >
    <el-form>
      <el-form-item label="名称">
        <el-input placeholder="请输入名称" v-model="MenuData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限值">
        <el-input placeholder="请输入权限值" v-model="MenuData.code"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button type="primary" @click="Save">确定</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </template>
  </el-dialog>
</template>
<script setup lang="ts">
//组合式api
import { ref, onMounted, reactive } from "vue";
//接口
import { getAllMenu, AddOrUpdateMenu, reqDeleteMenu } from "@/api/acl/menu";
//接口约束
import { getAllMenuDataRespones, addOrEditMenuData } from "@/api/acl/menu/type";
import { ElMessage } from "element-plus";
//定义数组接收所有返回的数据
let MenuList = ref<any>([]);
//控制对话框的显示与隐藏
let dialogVisible = ref<boolean>(false);
//收集添加菜单的数据
let MenuData = reactive<addOrEditMenuData>({
  code: "",
  level: 0,
  name: "",
  pid: 0,
});
//获取全部菜单数据
const AllMenuData = async () => {
  let result: getAllMenuDataRespones = await getAllMenu();
  if (result.code == 200) {
    //收集数据
    MenuList.value = result.data;
  }
};
//挂载时获取全部菜单数据
onMounted(() => {
  AllMenuData();
});
//添加菜单的回调
const AddPermisstion = (row: any) => {
  //清除上次数据
  Object.assign(MenuData, {
    code: "",
    level: 0,
    name: "",
    pid: 0,
    id: 0,
  });

  //显示对话框
  dialogVisible.value = true;
  //收集数据
  MenuData.level = row.level + 1;
  MenuData.pid = row.id;
};
//修改菜单的回调
const updatePermisstion = (row: any) => {
  //显示对话框
  dialogVisible.value = true;
  //收集数据
  Object.assign(MenuData, row);
};
//确定按钮的回调
const Save = async () => {
  //发请求
  let result = await AddOrUpdateMenu(MenuData);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: MenuData.id ? "修改成功" : "添加成功",
    });
    //关闭对话框
    dialogVisible.value = false;
    //重新获取数据
    AllMenuData();
  } else {
    ElMessage({
      type: "error",
      message: MenuData.id ? "修改失败" : "添加失败",
    });
  }
};
//删除按钮的回调
const DeleteMenu = async (id: number) => {
  let result = await reqDeleteMenu(id);
  if (result.code == 200) {
    ElMessage({
      type: "success",
      message: "删除成功",
    });
    //再次获取数据
    AllMenuData();
  } else {
    ElMessage({
      type: "error",
      message: "删除失败",
    });
  }
};
</script>
